 /**
 * jims
 */
package com.jims.master.modules.orders.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.orders.entity.DrugDispRec;
import com.jims.master.modules.orders.service.DrugDispRecService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 摆药记录Controller
 *
 * @author 陈晓阳
 * @version 2017-04-07
 */
@Controller
@RequestMapping(value = "${adminPath}/orders/drugDispRec")
public class DrugDispRecController extends BaseController {

    @Autowired
    private DrugDispRecService drugDispRecService;

    @ModelAttribute
    public DrugDispRec get(@RequestParam(required = false) String id) {
        DrugDispRec entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = drugDispRecService.get(id);
        }
        if (entity == null) {
            entity = new DrugDispRec();
        }
        return entity;
    }

  @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(DrugDispRec drugDispRec, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugDispRec> page = drugDispRecService.findPage(new Page<DrugDispRec>(request, response), drugDispRec);
        model.addAttribute("page", page);
        return "modules/orders/drugDispRecIndex";
    }



    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(DrugDispRec drugDispRec, Model model) {
        model.addAttribute("drugDispRec", drugDispRec);
        return "modules/orders/drugDispRecForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(DrugDispRec drugDispRec, Model model) {
        StringData data = new StringData();
        int i = drugDispRecService.save(drugDispRec);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存摆药记录成功");
        } else {
            data.setCode("error");
            data.setData("保存摆药记录失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(DrugDispRec drugDispRec) {
        StringData data = new StringData();
        int i = drugDispRecService.delete(drugDispRec);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除摆药记录成功");
        } else {
            data.setCode("error");
            data.setData("删除摆药记录失败");
        }
        return data;
    }

}